package com.xjscrm.console.service.common.menu.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.common.entity.SysMenu;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.mapper.SysMenuMapper;
import com.xjscrm.console.param.menu.SysMenuInfoDto;
import com.xjscrm.console.service.common.menu.MenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    private static Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Override
    public SysMenu selectByAppKey(SysMenuInfoDto sysMenuInfoDto) {
        SysMenu sysMenu=  sysMenuMapper.selectByAppKey(sysMenuInfoDto.getAppKey());
        return sysMenu;
    }

    @Override
    public void uploadSysMenu(SysMenuInfoDto sysMenuInfoDto) {
        if(StringUtils.isBlank(sysMenuInfoDto.getAppKey())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "平台KEY不能为空");
        }
        if(StringUtils.isBlank(sysMenuInfoDto.getRoutes())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "平台KEY不能为空");
        }

        if(StringUtils.isBlank(sysMenuInfoDto.getPassword())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入验证密码");
        }

        if(!"e9ad597a-0021-4e37-9706-c1ea96ca2ad7".equals(sysMenuInfoDto.getPassword())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "验证密码不正确");
        }

        SysMenu sysMenu = sysMenuMapper.selectByAppKey(sysMenuInfoDto.getAppKey());


        //新增
        if(sysMenu == null){
            sysMenu = new SysMenu();
            BeanUtils.copyProperties(sysMenuInfoDto, sysMenu);
            sysMenuMapper.add(sysMenu);
        }else{//修改
            sysMenu.setIcon(sysMenuInfoDto.getIcon());
            sysMenu.setUrl(sysMenuInfoDto.getUrl());
            sysMenu.setRemark(sysMenuInfoDto.getRemark());
            sysMenu.setName(sysMenuInfoDto.getName());
            sysMenu.setRoutes(sysMenuInfoDto.getRoutes());
            sysMenu.setPKey(sysMenuInfoDto.getPKey());
            sysMenuMapper.update(sysMenu);
        }
    }

    @Override
    public Map<String, JSONObject> analysis(@MerId Long merId, JSONObject jsonObject, Map<String, JSONObject> result) {

        try {

            Set<String> keys = jsonObject.keySet();
            if (keys.contains("type")) {
                if ("api".equals(jsonObject.getString("type"))) {
                    JSONObject jsonObjectDmp = new JSONObject();
                    jsonObjectDmp.put("auth", jsonObject.getString("auth"));
                    jsonObjectDmp.put("pKey", jsonObject.getString("pKey"));
                    result.put(jsonObject.getString("path"), jsonObjectDmp);
                }
            }

            for (String key : keys) {
                Object value = jsonObject.get(key);
                if (value instanceof JSONObject) {
                    JSONObject valueJsonObject = (JSONObject) value;
                    analysis(merId, valueJsonObject, result);
                } else if (value instanceof JSONArray) {
                    JSONArray jsonArray = (JSONArray) value;
                    if (jsonArray.size() > 0) {
                        analysisJSONArray(merId, jsonArray, result);
                    }
                }
            }

        } catch (Exception e) {
            logger.error("角色授权JSON解析出错！routes={}", jsonObject.toJSONString());
        }

        return result;
    }


    public void analysisJSONArray(Long merId, JSONArray jsonArray, Map<String, JSONObject> result) {

        for (Object json : jsonArray) {
            if (json instanceof JSONObject) {
                JSONObject valueJsonObject = (JSONObject) json;
                analysis(merId, valueJsonObject, result);
            } else if (json instanceof JSONArray) {
                JSONArray tmpJsonArray = (JSONArray) json;
                if (tmpJsonArray.size() >= 0) {
                    analysisJSONArray(merId, tmpJsonArray, result);
                }
            }
        }
    }
}
